package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ShopTabEnum;
import com.ptszyxx.popose.databinding.FragmentMineShopMyBinding;
import com.ptszyxx.popose.module.main.mine.vm.MineShopMyVM;
import com.ysg.base.BaseFragment;
import com.ysg.base.BaseViewPagerAdapter;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;

import java.util.ArrayList;
import java.util.List;

public class MineShopMyFragment extends BaseFragment<FragmentMineShopMyBinding, MineShopMyVM> {
    public static MineShopMyFragment getInstance() {
        return new MineShopMyFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_shop_my;
    }

    @Override
    public int initVariableId() {
        return 0;
    }

    @Override
    public MineShopMyVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MineShopMyVM.class);
    }

    @Override
    public void initView() {
        super.initView();
        String userPic = YSPUtils.getInstance().getUserPic();
        YImageUtil.show(binding.ivAvatar, userPic);
        initViewPager();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onPicEvent.observe(this, o -> setIvFrameUI((String)o));
    }


    private void setIvFrameUI(String url) {
        if (YStringUtil.isNotEmpty(url)){
            YImageUtil.show(binding.ivHeadFram, url);
            binding.ivHeadFram.setVisibility(View.VISIBLE);
        }else{
            binding.ivHeadFram.setVisibility(View.GONE);
        }
    }

    private List<Fragment> fragments = new ArrayList<>();
    private void initViewPager() {
        fragments.add(new MineShopTabFragment(ShopTabEnum.avatar_my));
        fragments.add(new MineShopTabFragment(ShopTabEnum.mounts_my));

        binding.rgType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rb_touxiangkuang) {
                    binding.mViewPager.setCurrentItem(0);
                }else if (i == R.id.rb_zuoji) {
                    binding.mViewPager.setCurrentItem(1);
                }
            }
        });

        binding.mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        binding.rgType.check(R.id.rb_touxiangkuang);
                        break;
                    case 1:
                        binding.rgType.check(R.id.rb_zuoji);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        BaseViewPagerAdapter pagerAdapter = new BaseViewPagerAdapter(getChildFragmentManager(), fragments);
        binding.mViewPager.setAdapter(pagerAdapter);
        binding.mViewPager.setCurrentItem(0);
    }

}
